<!DOCTYPE html>
<html>
<head>
    {include file="layout:_meta_index" /}
    <meta name="csrf-token" content="{:token()}">
    <link rel="stylesheet" href="__static__/admin/style/login.css" media="all">
</head>
<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main" bgimg>
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>通用后台</h2>
            <p>通用后台系统管理</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
                <input type="text" name="username" id="LAY-user-login-username" lay-verify="required|username" placeholder="用户名" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required|password" placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
                        <input type="text" name="verify" id="LAY-user-login-vercode" lay-verify="required" lay-reqtext="请输入右边的图形码" placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="{:captcha_src('login')}" onclick="this.src='{:captcha_src(\'login\')}?'+Math.random();" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input type="checkbox" name="remember" id="LAY-user-login-remember" lay-skin="primary" title="记住密码">
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit id="LAY-user-login-submit" lay-filter="LAY-user-login-submit">登 入</button>
            </div>

        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">
        <p>{:config('site.copyright')}</p>
    </div>
</div>

<script src="__static__/layui/layui.js"></script>
<script>
    layui.config({
        base: '__static__/admin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','util'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,form = layui.form;

        //增加csrf验证
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

        //是否记住密码
        is_remember();
        function is_remember(){
            $.ajax({
                async: true,
                type:'post',
                url: "{:url('index/remember')}",
                data: {},
                success: function (data) {
                    //console.log(data);
                    if (data.data.username) {
                        $('#LAY-user-login-username').val(data.data.username);
                        $('#LAY-user-login-password').val(data.data.password);
                        $('#LAY-user-login-remember').attr('checked', true);
                        form.render();
                    }
                }
            });
        }
        //表单验证
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                let msg;
                $.ajax({
                    async: false,
                    type:'post',
                    url: "{:url('index/check')}",
                    data: {username: value},
                    success: function (data) {
                        if (data.code==0) {
                            msg = data.msg;
                        }
                    }
                });
                return msg;
            },

            password: [
                /^[\S]{6,12}$/
                ,'请输入6到16位的密码'
            ],
        });

        //回车监听
        document.onkeydown = function (event) {
            var e = event || window.event;
            if (e && e.keyCode == 13) { //回车键的键值为13
                $('#LAY-user-login-submit').click();
            }
        };

        //提交
        form.on('submit(LAY-user-login-submit)', function(obj){
            $.post("{:url('index/login')}", obj.field, function (res) {
                var icon = 5;
                if (res.code) {
                    icon = 6;
                }
                layer.msg(res.msg, {icon: icon, time: 1500}, function () {   //提示的插件，可以使用alert代替
                    if (res.code) {
                        setTimeout(function () {
                            location.href="{:url('index/index')}";
                        }, 500);
                    } else {
                        $('#LAY-user-get-vercode').click();
                        //刷新csrf验证
                        $.ajaxSetup({headers: {'X-CSRF-TOKEN': res.data.token}});
                    }
                });
            }, 'json');
        });

    });
</script>
</body>
</html>